package week6;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main02
{
	public static void main(String[] args) 
	{
		Scanner in=new Scanner(System.in);
		int n=Integer.parseInt(in.nextLine());
		Person[] persons=new Person[n];
		for (int i = 0; i < n; i++) 
		{
			persons[i] = new Person(in.next(), in.nextInt());
		}
		Arrays.sort(persons,new Comparator<Person>(){
			public int compare(Person o1,Person o2)
			{if(!o1.name.equals(o2.name))
				return o1.name.compareTo(o2.name);
			return o2.age-o1.age; }
			});
		for (Person e : persons) 
		{
			System.out.println(e);
		}
	}
}
